package com.legoo.studaychineseonline.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.legoo.studaychineseonline.R;
import com.legoo.studaychineseonline.bean.CategoryTypeBean;
import com.legoo.studaychineseonline.bean.DifficultListBean;

import java.util.ArrayList;
import java.util.List;


public class ChooseClassContentLevelPopupWindowAdapter extends RecyclerView.Adapter<ChooseClassContentLevelPopupWindowAdapter.ViewHolder> /*implements View.OnClickListener*/{
    private OnPopupWindowLevelItemClickListener mOnItemClickListener = null;
    private LayoutInflater mInflater;
    List<DifficultListBean.DataBean.DifficultyListBean> childListBeans;
    private List<Boolean> isClicks= new ArrayList<>();//控件是否被点击,默认为false，如果被点击，改变值，控件根据值改变自身颜色

    public ChooseClassContentLevelPopupWindowAdapter(Context context, List<DifficultListBean.DataBean.DifficultyListBean> childListBeans) {
        mInflater = LayoutInflater.from(context);
        this.childListBeans = childListBeans;
        for(int i = 0;i<childListBeans.size();i++){
            isClicks.add(false);
        }
    }
    public void setDatas( List<DifficultListBean.DataBean.DifficultyListBean> childListBeans) {
        this.childListBeans = childListBeans;

    }
    public void setOnItemClickLitener(OnPopupWindowLevelItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
    @Override
    public int getItemCount() {
        return childListBeans.size();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.item_popupwindow, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.tvAddDevice = (TextView) view.findViewById(R.id.tv_content);
        //将创建的View注册点击事件
        //   view.setOnClickListener(this);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
        viewHolder.tvAddDevice.setText(childListBeans.get(i).getName_en());
        //将数据保存在itemView的Tag中，以便点击时进行获取
        viewHolder.itemView.setTag(viewHolder.tvAddDevice);
        if (isClicks.size()>0){
            if(isClicks.get(i)){
                viewHolder.tvAddDevice.setTextColor(Color.parseColor("#d5141f"));
            }else{
                viewHolder.tvAddDevice.setTextColor(Color.parseColor("#000000"));
            }
        }else {
            viewHolder.tvAddDevice.setTextColor(Color.parseColor("#000000"));
        }

//        if(addDevice.isFirst()) {
//            viewHolder.viewSpace.setVisibility(View.GONE);
//        }
        // 如果设置了回调，则设置点击事件
        if (mOnItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for(int i = 0; i <isClicks.size();i++){
                        isClicks.set(i,false);
                    }
                    isClicks.set(i,true);
                    notifyDataSetChanged();
                    mOnItemClickListener.onPopupWindowLevelItemClick(viewHolder.itemView,viewHolder.tvAddDevice, i);
                }
            });
        }
    }
    /** @Override
     public void onClick(View v) {
         if (mOnItemClickListener != null) {
             //注意这里使用getTag方法获取数据
             mOnItemClickListener.onItemClick(, textView,(int)v.getTag());
         }
     }*/

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        TextView tvAddDevice;
    }
    public interface OnPopupWindowLevelItemClickListener {
        void onPopupWindowLevelItemClick(View view, TextView textView, int position);
    }
}
